package cn.edu.ncu.housinghub.entity.house;

import cn.edu.ncu.housinghub.dto.request.house.HouseInfoUpdateRequest;
import cn.edu.ncu.housinghub.dto.request.house.HouseRequest;
import cn.edu.ncu.housinghub.entity.user.Agent;
import cn.edu.ncu.housinghub.entity.user.Landlord;
import jakarta.validation.constraints.*;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Max;

/**
 * 房源表（房源基础信息）
 */
@Data
public class House {
    private Long houseId; // 主键
    @NotNull(message = "房源标题不能为空")
    private String houseTitle; // 房源标题
    // 添加默认构造函数，确保嵌套对象能正确初始化
    public House() {
        this.landlord = new Landlord();
        this.agent = new Agent();
        this.complementaryFacilities = new ComplementaryFacilities();
        this.propertyVerification = new PropertyVerification();
        this.houseMedia = new HouseMedia();
    }

    @NotNull(message = "房东信息不能为空")
    private Landlord landlord; // 房东ID

    @NotBlank(message = "房源简介不能为空")
    private String houseSummary; // 房源简介

    // 房源只能由房东代理或中介代理，但不能同时为空
    private Agent agent; // 中介ID

    private ComplementaryFacilities complementaryFacilities; // 配套设施
    private PropertyVerification propertyVerification; // 房产认证

    @NotBlank(message = "房源位置不能为空")
    private String houseLocation; // 房源位置

    @NotNull(message = "户型不能为空")
    @Min(value = 1, message = "户型必须为正整数")
    @Max(value = 5, message = "户型必须为有效值")
    private Integer layout; // 户型（1=一居室，2=两居室，3=三居室，4=四居室，5=五居室）

    @NotNull(message = "面积不能为空")
    @Positive(message = "面积必须大于0")
    private BigDecimal area; // 面积（平方米）

    // 押金类型
    @NotNull(message = "押金类型不能为空")
    // 押金类型：市面上常见的押金类型（1=押1付1，2=押1付3，3=年押金）
    @Min(value = 1, message = "押金类型必须为有效值")
    @Max(value = 3, message = "押金类型必须为有效值")
    private Integer depositType;

    @NotNull(message = "楼层不能为空")
    // 使用校验约束楼层格式 所在楼层（int）/总楼层（int）
    @Pattern(regexp = "^\\d+/\\d+$", message = "楼层格式错误")
    private String floor; // 所在楼层

    @NotBlank(message = "朝向不能为空")
    private String orientation; // 朝向

    @NotNull(message = "状态不能为空")
    @Min(value = 0, message = "状态必须为有效值")
    @Max(value = 2, message = "状态必须为有效值")
    private Integer status; // 状态（0=未租，1=已租,2=已下架）

    @NotNull(message = "租金不能为空")
    @Positive(message = "租金必须大于0")
    private BigDecimal rent; // 租金（元/月）

    @NotNull(message = "支付方式不能为空")
    @Min(value = 1, message = "支付方式必须为有效值")
    @Max(value = 3, message = "支付方式必须为有效值")
    private Integer paymentType; // 支付方式（1=月付，2=季付，3=年付）

    @NotNull(message = "出租类型不能为空")
    // 出租类型（1=房东直售，2=中介代理）
    @Min(value = 1, message = "出租类型必须为有效值")
    @Max(value = 2, message = "出租类型必须为有效值")
    private Integer houseRentType;



    private String subwayInfo; // 地铁信息
    private LocalDateTime createTime; // 创建时间
    private Boolean isDeleted; // 软删除标记

    @NotNull(message = "经纬度不能为空")
    @DecimalMin(value = "-180.0", message = "经度必须在-180到180之间")
    @DecimalMax(value = "180.0", message = "经度必须在-180到180之间")
    private BigDecimal longitude;//经度
    @NotNull(message = "经纬度不能为空")
    @DecimalMin(value = "-90.0", message = "纬度必须在-90到90之间")
    @DecimalMax(value = "90.0", message = "纬度必须在-90到90之间")
    private BigDecimal latitude;//纬度

    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号码格式错误")
    private String phone;


    private HouseMedia houseMedia; // 房源媒体信息

    private String realName; // 新增字段





    public House(HouseRequest houseRequest) {
        this.houseTitle=houseRequest.getHouseTitle();
        this.houseSummary = houseRequest.getHouseSummary();
        this.propertyVerification=new PropertyVerification(houseRequest.getPropertyVerifyId());
        this.houseLocation = houseRequest.getHouseLocation();
        this.layout = houseRequest.getLayout();
        this.floor = houseRequest.getFloor();
        this.orientation = houseRequest.getOrientation();
        this.rent = houseRequest.getRent();
        this.paymentType = houseRequest.getPaymentType();
        this.status=0;
        this.houseRentType =houseRequest.getRentType();
        this.subwayInfo = houseRequest.getSubwayInfo();
        this.isDeleted = false;
        this.createTime=LocalDateTime.now();
        this.phone=houseRequest.getPhone();
        this.longitude=houseRequest.getLongitude();
        this.latitude=houseRequest.getLatitude();
        this.depositType=houseRequest.getDepositType();
    }

    public House(HouseInfoUpdateRequest infoUpdateRequest) {
        this.houseId=infoUpdateRequest.getHouseId();
        this.houseTitle=infoUpdateRequest.getHouseTitle();
        this.houseSummary = infoUpdateRequest.getHouseSummary();
        this.houseLocation = infoUpdateRequest.getHouseLocation();
        this.layout = infoUpdateRequest.getLayout();
        this.floor = infoUpdateRequest.getFloor();
        this.orientation = infoUpdateRequest.getOrientation();
        this.rent = infoUpdateRequest.getRent();
        this.paymentType = infoUpdateRequest.getPaymentType();
        this.subwayInfo = infoUpdateRequest.getSubwayInfo();
        this.depositType=infoUpdateRequest.getDepositType();
        this.phone=infoUpdateRequest.getPhone();
    }

    public House(Long houseId) {
        this.houseId = houseId;
    }
}
